-- io.read()
-- io.write()

-- open
-- open a file in a certain way
-- "r" --> read 	"r+" --> read and write, if file not exist, error
-- "w" --> write (overwrite) 	"w+" --> read and write, if file not exist, create
-- "a" --> append 	"a+" --> read and write, if file not exist, create
-- "b" --> binary file (combine with other option, like, "rb", "r+b")
print(io.open("non-exist-file", "r")) 	-- error
print(io.open("/etc/passwd", "w")) 	 		-- permission denied

-- pre-defined file stream
-- io.stdin		standard input
-- io.stdout 	standard output
-- io.stderr 	standard error
io.stderr:write("this is io.stderr", "\n")

-- input/output stream
local tmp = io.input() 	-- get the current input stream
io.input("newinput") 		-- set a new input stream
io.input():close() 			-- close the current input stream
io.input(tmp) 					-- restore the previous input stream
-- io.output() is the same with io.input()

-- tmp file (auto remove)
local tmpfile = io.tmpfile() 	-- return a tmpfile input stream (rw)
io.input(tmpfile)
io.input():close()

-- flush (flush all the data in buffer into file)
io.output():flush()

-- seek (get or set the file location)
-- f:seek(whence, offset)
-- whence --> "cur" (current) default 
--  			--> "set" (start of file)
-- 				--> "end" (end of file)
-- offset --> 0 default 	(offset relative to whence)
function fsize (file)
	local current = file:seek() 	-- store the current position
	local size = file:seek("end") -- get the file size
	file:seek("set", current) 		-- restore the file location
	return size
end
